package com.sanpu.kxe.business.main.fragment;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanpu.library.imagechoose.PhotoPicker;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseFragment;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.CertificationOutput;
import com.sanpu.kxe.business.main.contract.AuthThirdStepContract;
import com.sanpu.kxe.business.main.presenter.AuthThirdStepPresenter;
import com.sanpu.kxe.business.main.provider.MainOpenProvider;
import com.sanpu.kxe.business.main.view.CertificateSecondActivity;
import com.sanpu.kxe.common.custom.SerializableHashMap;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.jump.eventbus.ReviewPhotoEvent;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.module.specialunit.OnClickableSpanListener;
import com.sanpu.kxe.module.specialunit.SimplifySpanBuild;
import com.sanpu.kxe.module.specialunit.unit.SpecialClickableUnit;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;

import static com.sanpu.kxe.R.id.crdConCloud;
import static com.sanpu.kxe.R.id.crdConIv;
import static com.sanpu.kxe.R.id.crdFroCloud;
import static com.sanpu.kxe.R.id.crdFroIv;
import static com.sanpu.kxe.R.id.handCloud;
import static com.sanpu.kxe.R.id.handIv;

/**
 * Description:认证第三步
 * Created by 薛勇军 on 2018/7/20.
 */

public class AuthThirdStepFragment extends BaseFragment implements AuthThirdStepContract.View {

    @Bind(crdFroIv)
    ImageView vCrdFroIv;
    @Bind(R.id.crdFro)
    ImageView vCrdFro;
    @Bind(crdFroCloud)
    TextView vCrdFroCloud;

    @Bind(crdConIv)
    ImageView vCrdConIv;
    @Bind(R.id.crdCon)
    ImageView vCrdCon;
    @Bind(crdConCloud)
    TextView vCrdConCloud;

    @Bind(handIv)
    ImageView vHandIv;
    @Bind(R.id.hand)
    ImageView vHand;
    @Bind(handCloud)
    TextView vHandCloud;

    @Bind(R.id.idCardClaimTv)
    TextView idCardClaimTv;

    private AuthThirdStepContract.Presenter mPresenter;

    private boolean isNeedUpload = true;

    private CertificationOutput mOutput;

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_auth_third;
    }

    @Override
    protected void initView() {
        super.initView();
        mPresenter = new AuthThirdStepPresenter(this);

        SpecialClickableUnit clickableUnit = new SpecialClickableUnit(new OnClickableSpanListener() {
            @Override
            public void onClick(TextView tv, String clickText) {
                mPresenter.openOldCertificateOneActivity();
            }
        });
        clickableUnit.setSpecialText("请点击使用人工审核");
        clickableUnit.setNormalTextColor(ContextCompat.getColor(getContext(), R.color.c_ff3f40));
        clickableUnit.setPressTextColor(ContextCompat.getColor(getContext(), R.color.c_ff3f40));
        clickableUnit.showUnderline();
        SimplifySpanBuild build = new SimplifySpanBuild(getContext(), idCardClaimTv, "4. 如照片一直未通过，");
        build.appendSpecialUnit(clickableUnit);
        idCardClaimTv.append(build.build());
    }

    @OnClick({R.id.commit, R.id.lastStepRtv, R.id.crdFro, R.id.crdCon, R.id.hand})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.commit:
                if (!isNeedUpload && mOutput != null) {
                    showBankInfo(mOutput, false);
                } else {
                    mPresenter.onCommitData();
                }
                break;
            case R.id.lastStepRtv:
                ((CertificateSecondActivity) getActivity()).switchPageSecond();
                break;
            case R.id.crdFro:
                if (vCrdFroIv.getTag() != null) {
                    mPresenter.takePhoto((String) vCrdFroIv.getTag(),
                            CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_CRDFRO);
                } else {
                    MainOpenProvider.getInstance().openCertificateHelpActivity(
                            getContext(), CommonConfig.TYPE_CRDFROPIC);
                }
                break;
            case R.id.crdCon:
                if (vCrdConIv.getTag() != null) {
                    mPresenter.takePhoto((String) vCrdConIv.getTag(),
                            CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_CRDCON);
                } else {
                    MainOpenProvider.getInstance().openCertificateHelpActivity(
                            getContext(), CommonConfig.TYPE_CRDCONPIC);
                }
                break;
            case R.id.hand:
                if (vHandIv.getTag() != null) {
                    mPresenter.takePhoto((String) vHandIv.getTag(),
                            CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_HAND);
                } else {
                    MainOpenProvider.getInstance().openCertificateHelpActivity(
                            getContext(), CommonConfig.TYPE_IDHANDPIC);
                }
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        CertificationOutput certificationOutput = ((CertificateSecondActivity) getActivity()).getCertificationOutput();
        SerializableHashMap hashMap = ((CertificateSecondActivity) getActivity()).getSerializableHashMap();
        mPresenter.initViewData(certificationOutput, hashMap);
    }

    @Override
    public void showBankInfo(CertificationOutput output, boolean isNeedUpload) {
        this.mOutput = output;
        this.isNeedUpload = isNeedUpload;
        if (mOutput != null) {
            ((CertificateSecondActivity) getActivity()).setCertificationOutput(mOutput);
            MainOpenProvider.getInstance().openCertificateThirdActivity(getContext(), mOutput);
        }
    }

    @Override
    public void onActivityResult(final int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && data != null) {
            ArrayList<String> photos =
                    data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
            if (photos == null || photos.size() == 0) {
                switch (requestCode) {
                    case CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_CRDFRO:
                        vCrdFroIv.setTag(null);
                        vCrdFroIv.setImageDrawable(null);
                        vCrdFroIv.setVisibility(View.GONE);
                        vCrdFroCloud.setVisibility(View.GONE);
                        mPresenter.removeFile(CommonConfig.TYPE_CRDFROPIC);
                        isNeedUpload = true;
                        break;
                    case CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_CRDCON:
                        vCrdConIv.setTag(null);
                        vCrdConIv.setImageDrawable(null);
                        vCrdConIv.setVisibility(View.GONE);
                        vCrdConCloud.setVisibility(View.GONE);
                        mPresenter.removeFile(CommonConfig.TYPE_CRDCONPIC);
                        isNeedUpload = true;
                        break;
                    case CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_HAND:
                        vHandIv.setTag(null);
                        vHandIv.setImageDrawable(null);
                        vHandIv.setVisibility(View.GONE);
                        vHandCloud.setVisibility(View.GONE);
                        mPresenter.removeFile(CommonConfig.TYPE_IDHANDPIC);
                        isNeedUpload = true;
                        break;
                }
            }
        }
    }

    @Override
    public void refreshView(ReviewPhotoEvent event) {
        String filePath = event.getFilePath();
        if (TextUtils.isEmpty(filePath)) {
            return;
        }
        String fileIdName;
        switch (event.getCurrentType()) {
            case CommonConfig.TYPE_CRDFROPIC:
                fileIdName = CommonConfig.TYPE_CRDFROPIC;
                JKImageLoader.getInstance().displayImage(getContext(), "file://" + filePath, vCrdFroIv);
                vCrdFroIv.setTag(filePath);
                vCrdFroIv.setVisibility(View.VISIBLE);
                vCrdFroCloud.setVisibility(View.VISIBLE);
                isNeedUpload = true;
                break;
            case CommonConfig.TYPE_CRDCONPIC:
                fileIdName = CommonConfig.TYPE_CRDCONPIC;
                JKImageLoader.getInstance().displayImage(getContext(), "file://" + filePath, vCrdConIv);
                vCrdConIv.setTag(filePath);
                vCrdConIv.setVisibility(View.VISIBLE);
                vCrdConCloud.setVisibility(View.VISIBLE);
                isNeedUpload = true;
                break;
            case CommonConfig.TYPE_IDHANDPIC:
                fileIdName = CommonConfig.TYPE_IDHANDPIC;
                JKImageLoader.getInstance().displayImage(getContext(), "file://" + filePath, vHandIv);
                vHandIv.setTag(filePath);
                vHandIv.setVisibility(View.VISIBLE);
                vHandCloud.setVisibility(View.VISIBLE);
                isNeedUpload = true;
                break;
            default:
                return;
        }
        mPresenter.onCompress(filePath, fileIdName);
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(AuthThirdStepContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void showLoadingDialog(boolean cancelable, String content) {
        ((BaseTitleActivity)getActivity()).showLoadingDialog(cancelable, content);
    }

    @Override
    public void dismissLoadingDialog() {
        ((BaseTitleActivity)getActivity()).dismissLoadingDialog();
    }

    @Override
    public void showErrorText(Throwable e) {
        ((BaseTitleActivity)getActivity()).showErrorText(e);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
